package com.cloudea.learn.component;

import com.cloudea.learn.Tensor;
import com.cloudea.learn.component.Component;
import com.cloudea.learn.layer.ElementAdd;
import com.cloudea.learn.layer.MatDot;

/**
 * 线性变换层，默认没有激活函数
 * @author cloudea
 * @date 2021/12/21
 */
public class Linear extends Component {
    private final Tensor W;
    private final Tensor b;
    private final MatDot matDot;
    private final ElementAdd elementAdd;

    public Linear(int inputSize, int outputSize){
        this.W = createParameter(outputSize, inputSize);
        this.b = createParameter(outputSize);
        this.matDot = new MatDot();
        this.elementAdd = new ElementAdd();
    }

    @Override
    public Tensor forward(Tensor... input) {
        super.forward(input);
        Tensor x = input[0];
        x = matDot.call(W, x);
        x = elementAdd.call(x, b);
        return x;
    }
}
